﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.LongER.Framework.Utils;
using Com.LongER.UI;
using Com.LongER.Business.ZHM002;
using Com.LongER.Framework.Utils.LogUtils;

public partial class ZHM002 : ABasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //
        if (!this.Page.IsPostBack)
        {
            //画面ID
            Label lblPageId = (Label)Master.FindControl("lblPageId");
            lblPageId.Text = "ZHM002";

            //画面处理名
            Label lblPageName = (Label)Master.FindControl("lblPageName");
            lblPageName.Text = "用户详细管理画面";

            //清空画面中的输入框
            #region
            proc清空_Init("0");
            #endregion
        }

        //autocomplete
        this.txt所属区分.Text = this.hid所属区分.Value;
        this.txt部门.Text = this.hid部门.Value;
        this.txt职位.Text = this.hid职位.Value;
    }

    //
    //
    //
    //
    protected void btn主菜单_Click(object sender, EventArgs e)
    {
        Response.Redirect("/LongER/ZAH003.aspx");
    }

    protected void btn执行_Click(object sender, EventArgs e)
    {
        //初期化处理
        #region
        //错误信息
        Label lblMsgCD = (Label)Master.FindControl("lblMsgCD");
        Label lblMsg = (Label)Master.FindControl("lblMsg");
        lblMsgCD.Text = "";
        lblMsg.Text = "";

        #endregion

        //处理区分进行确认
        if (this.txt处理区分.Text == string.Empty) //处理区分为空的场合
        {
            //String msg = MessageUtil.GetMessage("GTH000", "IGTH000001");
            //lblMsgCD.Text = "IGTH000001";
            //lblMsg.Text = msg;
            this.SetMessage("GTH000", "IGTH000001", MSG_LEVEL.Info);
            this.AddCssClass(this.txt处理区分, CSS_TEXT_ERROR);
            return;
        }
        else if (this.txt处理区分.Text == "1") //处理区分为1(查询)的场合
        {
            #region
            //查询时，各个控件的属性进行控制
            proc清空_Init(this.txt处理区分.Text);

            //用户ID为空时
            if (this.txt用户ID.Text == string.Empty)
            {
                //String msg = MessageUtil.GetMessage("GTH002", "IGTH002002");
                //lblMsgCD.Text = "IGTH002002";
                //lblMsg.Text = msg;
                this.SetMessage("GTH002", "IGTH002002", MSG_LEVEL.Error);
                this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
                return;
            }

            //通过用户ID进行查询
            CZHM002 instance = new CZHM002();
            ZHM002DS.M_USERDETAILDataTable dt = instance.GetMUSerDetail(this.txt用户ID.Text);
            if (dt.Count <= 0)
            {
                //查询失败
                //String msg = MessageUtil.GetMessage("GTH002", "IGTH002001");
                //lblMsgCD.Text = "IGTH002001";
                //lblMsg.Text = msg;
                this.SetMessage("GTH002", "IGTH002001", MSG_LEVEL.Error);
                this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
                this.txt用户ID.Focus();
                return;
            }
            else
            {
                //查询成功
                //String msg = MessageUtil.GetMessage("GTH002", "IGTH002000");
                //lblMsgCD.Text = "IGTH002000";
                //lblMsg.Text = msg;
                this.SetMessage("GTH002", "IGTH002000", MSG_LEVEL.Info);
                this.txt用户姓名.Text = dt[0].USERID_HANZI.ToString();
                if (dt[0].SHO.ToString() != string.Empty){
                    this.txt所属区分CD.Text = dt[0].SHO.ToString();
                }
                if (dt[0].BUMEN.ToString() != string.Empty){
                    this.txt部门CD.Text = dt[0].BUMEN.ToString();
                }
                if (dt[0].ZHIWEI.ToString() != string.Empty){
                    this.txt职位CD.Text = dt[0].ZHIWEI.ToString();
                }

                this.txt密码.Text = dt[0].PWD.ToString();
                this.txt密码有效期.Text = dt[0].PWD_YOUXIAO_ADMMDD.ToString();

                if (dt[0].MAIL_ADDR.ToString() != string.Empty){
                    this.txt邮箱.Text = dt[0].MAIL_ADDR.ToString();
                }
                this.txt用户ID.Focus();
            }
            #endregion

        }
        else if (this.txt处理区分.Text == "2") //处理区分为2(登记)的场合
        {
            #region
            //登记时，各个控件的值进行检查

            //用户ID的值进行检查
            #region
            if (this.txt用户ID.Text.Trim() == string.Empty)
            {
                //用户ID为空的场合,退出登陆
                //String msg = MessageUtil.GetMessage("ZHM002", "IZHM002000");
                //lblMsgCD.Text = "IZHM002000";
                //lblMsg.Text = msg;
                this.SetMessage("ZHM002", "IZHM002000", MSG_LEVEL.Error);
                this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
                return;
            }
            else {
                //对用户ID进行查询
                CZHM002 instanceSelectMUser = new CZHM002();
                ZHM002DS.M_USERDETAILDataTable dtSelectMUser = instanceSelectMUser.GetMUSerDetail(this.txt用户ID.Text);
                if (dtSelectMUser.Count > 0)
                {
                    //用户ID已存在场合,退出登陆
                    //String msg = MessageUtil.GetMessage("ZHM002", "IZHM002001");
                    //lblMsgCD.Text = "IZHM002001";
                    //lblMsg.Text = msg;
                    this.SetMessage("ZHM002", "IZHM002001", MSG_LEVEL.Error);
                    this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
                    this.txt用户ID.Focus();
                    return;
                }
            }
            #endregion

            //员工ID的值进行检查
            #region
            //员工ID的值不为空，并且员工ID必须在员工表中存在
            if (this.txt员工ID.Text.Trim() == string.Empty)
            {
                //员工ID为空的场合，退出登陆
                //String msg = MessageUtil.GetMessage("ZHM002", "IZHM002002");
                //lblMsgCD.Text = "IZHM002002";
                //lblMsg.Text = msg;
                this.SetMessage("ZHM002", "IZHM002002", MSG_LEVEL.Error);
                this.AddCssClass(this.txt员工ID, CSS_TEXT_ERROR);
                return;
            }
            else
            {
                //对员工ID进行查询
                CZHM002 instanceSelectStaff = new CZHM002();
                ZHM002DS.M_STAFFDETAILDataTable dtSelectStaff = instanceSelectStaff.GetMStaffDetail(this.txt员工ID.Text);
                if (dtSelectStaff.Count <= 0)
                {
                    //员工ID在员工表中不存在的场合，退出登陆
                    //String msg = MessageUtil.GetMessage("ZHM002", "IZHM002003");
                    //lblMsgCD.Text = "IZHM002003";
                    //lblMsg.Text = msg;
                    this.SetMessage("ZHM002", "IZHM002003", MSG_LEVEL.Error);
                    this.AddCssClass(this.txt员工ID, CSS_TEXT_ERROR);
                    this.txt员工ID.Focus();
                    return;
                }
                else 
                {
                    //this.txt用户姓名.Text = dtSelectStaff[0].
                    if ((dtSelectStaff[0].SHO != string.Empty) && (dtSelectStaff[0].SHO_HANZI != string.Empty))
                    {
                        this.txt所属区分CD.Text = dtSelectStaff[0].SHO.ToString();
                        this.txt所属区分.Text = dtSelectStaff[0].SHO_HANZI.ToString();
                    }
                    if ((dtSelectStaff[0].BUMEN != string.Empty) && (dtSelectStaff[0].BUMEN_HANZI != string.Empty))
                    {
                    this.txt部门CD.Text = dtSelectStaff[0].BUMEN.ToString();
                    this.txt部门.Text = dtSelectStaff[0].BUMEN_HANZI.ToString();
                    }
                    if ((dtSelectStaff[0].ZHIWEI != string.Empty) && (dtSelectStaff[0].ZHIWEI_HANZI != string.Empty))
                    {
                    this.txt职位CD.Text = dtSelectStaff[0].ZHIWEI.ToString();
                    this.txt职位.Text = dtSelectStaff[0].ZHIWEI_HANZI.ToString();
                    }

                    if (dtSelectStaff[0].MOBIL1 != string.Empty) 
                    {
                    this.txt联系电话1.Text = dtSelectStaff[0].MOBIL1.ToString();
                    }

                    //if(! Convert.IsDBNull(dtSelectStaff[0].MOBIL2)) 
                    //{
                    //    this.txt联系电话2.Text = dtSelectStaff[0].MOBIL2.ToString();
                    //}

                    if (dtSelectStaff[0].MAIL_ADDR != string.Empty) 
                    {
                    this.txt邮箱.Text = dtSelectStaff[0].MAIL_ADDR.ToString();
                    }
                }
            }
            #endregion


            //执行登记
            #region
            Boolean isInserted = false;
            CZHM002 instance = new CZHM002();
            isInserted = instance.InsertMUSer(this.txt用户ID.Text, this.txt员工ID.Text, this.txt用户姓名.Text
                                                 , this.txt密码.Text, this.txt密码有效期.Text);
            if (!isInserted)
            {
                //登记失败
                //String msg = MessageUtil.GetMessage("GTH003", "IGTH003002");
                //lblMsgCD.Text = "IGTH003002";
                //lblMsg.Text = msg;
                this.SetMessage("GTH003", "IGTH003002", MSG_LEVEL.Error);
                this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
                this.txt用户ID.Focus();
            }
            else
            {
                //登记成功
                //String msg = MessageUtil.GetMessage("GTH003", "IGTH003000");
                //lblMsgCD.Text = "IGTH003000";
                //lblMsg.Text = msg;
                this.SetMessage("GTH003", "IGTH003000", MSG_LEVEL.Info);
                this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
                this.txt用户ID.Focus();
            }
            #endregion

            #endregion
        }
        else if (this.txt处理区分.Text == "3") //处理区分为1(订正)的场合
        {
            #region
            //查询时，各个控件的属性进行控制
            proc清空_Init(this.txt处理区分.Text);
            //对更新对象进行确认

            //执行更新
            Boolean isChanged = false;
            CZHM002 instance = new CZHM002();
            isChanged = instance.UpdateMUserMStaff(this.txt用户ID.Text, this.txt员工ID.Text, this.txt用户姓名.Text
                                                 ,this.txt密码.Text,this.txt密码有效期.Text
                                                 , this.txt所属区分CD.Text, this.txt部门CD.Text, this.txt职位CD.Text
                                                 , this.txt联系电话1.Text, this.txt联系电话2.Text,this.txt邮箱.Text);
            if (!isChanged)
            {
                //更新失败
                String msg = MessageUtil.GetMessage("GTH001", "IGTH001002");
                lblMsgCD.Text = "IGTH001002";
                lblMsg.Text = msg;
                this.txt用户ID.Focus();
            }
            else
            {
                //更新成功
                String msg = MessageUtil.GetMessage("GTH001", "IGTH001000");
                lblMsgCD.Text = "IGTH001000";
                lblMsg.Text = msg;
                this.txt用户ID.Focus();
            }
            #endregion

        }
        else if (this.txt处理区分.Text == "9") //处理区分为1(删除)的场合
        {
            #region
            //删除时，各个控件的属性进行控制
            proc清空_Init(this.txt处理区分.Text);

            //执行删除
            Boolean isDeleted = false;
            CZHM002 instance = new CZHM002();
            isDeleted = instance.DeleteMUSer(this.txt用户ID.Text);
            if (!isDeleted)
            {
                //删除失败
                String msg = MessageUtil.GetMessage("GTH004", "IGTH004002");
                lblMsgCD.Text = "IGTH004002";
                lblMsg.Text = msg;
                this.txt用户ID.Focus();
            }
            else
            {
                //删除成功
                String msg = MessageUtil.GetMessage("GTH004", "IGTH004000");
                lblMsgCD.Text = "IGTH004000";
                lblMsg.Text = msg;
                this.txt用户ID.Focus();
            }

            #endregion
        }
        else //处理区分为上述以外的场合
        {
            String msg = MessageUtil.GetMessage("GTH000", "IGTH000002");
            lblMsgCD.Text = "IGTH000002";
            lblMsg.Text = msg;
            return;
        }

    }

    protected void proc清空_Init(string str处理区分)
    {
        Label lblMsgCD = (Label)Master.FindControl("lblMsgCD");
        Label lblMsg = (Label)Master.FindControl("lblMsg");
        lblMsgCD.Text = "";
        lblMsg.Text = "";
        //this.SetMessage("", "", MSG_LEVEL.Info);

        //初期化
        if (str处理区分 == "0")
        {
            //控件的Enabled进行控制
            #region
            this.txt用户ID.Enabled = true;

            this.txt员工ID.Enabled = false;
            this.txt用户姓名.Enabled = false;
            this.txt所属区分CD.Enabled = false;
            this.txt所属区分.Enabled = false;
            this.txt部门CD.Enabled = false;
            this.txt部门.Enabled = false;
            this.txt职位CD.Enabled = false;
            this.txt职位.Enabled = false;
            this.txt密码.Enabled = false;
            this.txt密码有效期.Enabled = false;
            this.txt联系电话1.Enabled = false;
            this.txt联系电话2.Enabled = false;
            this.txt邮箱.Enabled = false;
            #endregion

            //控件的Text进行清空
            #region
            this.txt处理区分.Text = string.Empty;
            this.txt处理区分.Focus();

            this.txt用户ID.Text = string.Empty;
            this.txt员工ID.Text = string.Empty;
            this.txt用户姓名.Text = string.Empty;
            this.txt所属区分CD.Text = string.Empty;
            this.txt所属区分.Text = string.Empty;
            this.txt部门CD.Text = string.Empty;
            this.txt部门.Text = string.Empty;
            this.txt职位CD.Text = string.Empty;
            this.txt职位.Text = string.Empty;
            this.txt密码.Text = string.Empty;
            this.txt密码有效期.Text = string.Empty;
            this.txt联系电话1.Text = string.Empty;
            this.txt联系电话2.Text = string.Empty;
            this.txt邮箱.Text = string.Empty;
            #endregion
        }
        
        //查询
        if (str处理区分 == "1" )
        {
            //控件的Enabled进行控制
            #region
            this.txt用户ID.Enabled = true;

            this.txt员工ID.Enabled = false;
            this.txt用户姓名.Enabled = false;
            this.txt所属区分CD.Enabled = false;
            this.txt所属区分.Enabled = false;
            this.txt部门CD.Enabled = false;
            this.txt部门.Enabled = false;
            this.txt职位CD.Enabled = false;
            this.txt职位.Enabled = false;
            this.txt密码.Enabled = false;
            this.txt密码有效期.Enabled = false;
            this.txt联系电话1.Enabled = false;
            this.txt联系电话2.Enabled = false;
            this.txt邮箱.Enabled = false;
            #endregion

            //控件的Text进行清空
            #region
            this.txt员工ID.Text = string.Empty;
            this.txt用户姓名.Text = string.Empty;
            this.txt所属区分CD.Text = string.Empty;
            this.txt所属区分.Text = string.Empty;
            this.txt部门CD.Text = string.Empty;
            this.txt部门.Text = string.Empty;
            this.txt职位CD.Text = string.Empty;
            this.txt职位.Text = string.Empty;
            this.txt密码.Text = string.Empty;
            this.txt密码有效期.Text = string.Empty;
            this.txt联系电话1.Text = string.Empty;
            this.txt联系电话2.Text = string.Empty;
            this.txt邮箱.Text = string.Empty;
            #endregion
        }

        //登记或订正
        if (str处理区分 == "2" || str处理区分 == "3")
        {
            //控件的Enabled进行控制
            #region
            this.txt用户ID.Enabled = true;

            this.txt员工ID.Enabled = true;
            this.txt用户姓名.Enabled = true;
            this.txt所属区分CD.Enabled = false;
            this.txt所属区分.Enabled = false;
            this.txt部门CD.Enabled = false;
            this.txt部门.Enabled = false;
            this.txt职位CD.Enabled = false;
            this.txt职位.Enabled = false;
            this.txt密码.Enabled = true;
            this.txt密码有效期.Enabled = true;
            this.txt联系电话1.Enabled = false;
            this.txt联系电话2.Enabled = false;
            this.txt邮箱.Enabled = false;
            #endregion

            //控件的Text进行清空
            #region
            //this.txt员工ID.Text = string.Empty;
            //this.txt用户姓名.Text = string.Empty;
            this.txt所属区分CD.Text = string.Empty;
            this.txt所属区分.Text = string.Empty;
            this.txt部门CD.Text = string.Empty;
            this.txt部门.Text = string.Empty;
            this.txt职位CD.Text = string.Empty;
            this.txt职位.Text = string.Empty;
            //this.txt密码.Text = string.Empty;
            //this.txt密码有效期.Text = string.Empty;
            this.txt联系电话1.Text = string.Empty;
            this.txt联系电话2.Text = string.Empty;
            this.txt邮箱.Text = string.Empty;
            #endregion
        }

        //删除
        if (str处理区分 == "9")
        {
            //控件的Enabled进行控制
            #region
            this.txt用户ID.Enabled = true;

            this.txt员工ID.Enabled = false;
            this.txt用户姓名.Enabled = false;
            this.txt所属区分CD.Enabled = false;
            this.txt所属区分.Enabled = false;
            this.txt部门CD.Enabled = false;
            this.txt部门.Enabled = false;
            this.txt职位CD.Enabled = false;
            this.txt职位.Enabled = false;
            this.txt密码.Enabled = false;
            this.txt密码有效期.Enabled = false;
            this.txt联系电话1.Enabled = false;
            this.txt联系电话2.Enabled = false;
            this.txt邮箱.Enabled = false;
            #endregion

            //控件的Text不进行清空
            #region
            //this.txt员工ID.Text = string.Empty;
            //this.txt用户姓名.Text = string.Empty;
            //this.txt所属区分CD.Text = string.Empty;
            //this.txt所属区分.Text = string.Empty;
            //this.txt部门CD.Text = string.Empty;
            //this.txt部门.Text = string.Empty;
            //this.txt职位CD.Text = string.Empty;
            //this.txt职位.Text = string.Empty;
            //this.txt密码.Text = string.Empty;
            //this.txt密码有效期.Text = string.Empty;
            //this.txt联系电话1.Text = string.Empty;
            //this.txt联系电话2.Text = string.Empty;
            //this.txt邮箱.Text = string.Empty;
            #endregion
        }
    }

    protected void txt处理区分_TextChange(object sender, EventArgs e)
    {
        Label lblMsgCD = (Label)Master.FindControl("lblMsgCD");
        Label lblMsg = (Label)Master.FindControl("lblMsg");

        //处理区分为空的场合
        if (this.txt处理区分.Text == string.Empty)
        {
            String msg = MessageUtil.GetMessage("GTH000", "IGTH000001");
            lblMsgCD.Text = "IGTH000001";
            lblMsg.Text = msg;
            return;
         }
        //处理区分为1,2,3,9的场合
        else if (this.txt处理区分.Text == "1" || this.txt处理区分.Text == "2" || 
            this.txt处理区分.Text == "3" || this.txt处理区分.Text == "9")
        {
            this.proc清空_Init(this.txt处理区分.Text);
        }
        //处理区分为上述以外的场合
        else
        {
            String msg = MessageUtil.GetMessage("GTH000", "IGTH000002");
            lblMsgCD.Text = "IGTH000002";
            lblMsg.Text = msg;
            return;
        }

    }

    protected void btn清除_Click(object sender, EventArgs e)
    {
        //初期化处理
        #region
        //查询时，各个控件的属性进行控制
        proc清空_Init("0");
        #endregion
    }


}